package com.zl.tesseract.scanner.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zl.tesseract.R;
import com.zl.tesseract.scanner.beans.BrandBean;

import java.util.List;

public class BrandListAdapter extends RecyclerView.Adapter<BrandListAdapter.ViewHolder> {

    private Context context;

    private OnItemClickListener mItemClickListener;
    private List<BrandBean> gradeList;

    public BrandListAdapter(Context context, List<BrandBean> gradeList) {
        this.context = context;
        this.gradeList = gradeList;
    }

    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_brand_list, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder holder, @SuppressLint("RecyclerView") final int position) {
        LinearLayout item = holder.item;
        ImageView iv_brand = holder.iv_brand;
        TextView tv_brand = holder.tv_brand;

//        setTextViewBrand(gradeList.get(position).getName(), tv_brand);
//        setImageViewBrand(gradeList.get(position).getName(), iv_brand);

        item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mItemClickListener.onItemClick(v, position);
//                for (BrandBean typeBean: gradeList) {
//                    typeBean.setSelect(false);
//                }
//                gradeList.get(position).setSelect(true);
//                notifyDataSetChanged();
            }
        });

//        if(gradeList.get(position).isSelect()){
//            tv_brand.setTextColor(ContextCompat.getColor(context, R.color.green_text));
//        }else{
//            tv_brand.setTextColor(ContextCompat.getColor(context, R.color.black_text_color));
//        }
    }

    @Override
    public int getItemCount() {
        return gradeList.size();
    }

    static class ViewHolder extends  RecyclerView.ViewHolder {
        LinearLayout item;
        ImageView iv_brand;
        TextView tv_brand;

        ViewHolder(View itemView) {
            super(itemView);
            item = itemView.findViewById(R.id.item);
            iv_brand = itemView.findViewById(R.id.iv_brand);
            tv_brand = itemView.findViewById(R.id.tv_brand);
        }
    }

    /**
     * item的点击事件
     */
    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    public void setItemClickListener(OnItemClickListener itemClickListener) {
        mItemClickListener = itemClickListener;
    }

    private void setImageViewBrand(String brandName, ImageView iv_brand) {
        switch(brandName){
            case "anneng":
                iv_brand.setImageResource(R.drawable.brand_anneng);
                break;
            case "baishi":
                iv_brand.setImageResource(R.drawable.brand_baishi);
                break;
            case "debang":
                iv_brand.setImageResource(R.drawable.brand_debang);
                break;
            case "ems":
                iv_brand.setImageResource(R.drawable.brand_ems);
                break;
            case "guotong":
                iv_brand.setImageResource(R.drawable.brand_guotong);
                break;
            case "jingdong":
                iv_brand.setImageResource(R.drawable.brand_jingdong);
                break;
            case "kuaijie":
                iv_brand.setImageResource(R.drawable.brand_kuaijie);
                break;
            case "quanfeng":
                iv_brand.setImageResource(R.drawable.brand_quanfeng);
                break;
            case "shentong":
                iv_brand.setImageResource(R.drawable.brand_shentong);
                break;
            case "shunfeng":
                iv_brand.setImageResource(R.drawable.brand_shunfeng);
                break;
            case "suning":
                iv_brand.setImageResource(R.drawable.brand_suning);
                break;
            case "tianmao":
                iv_brand.setImageResource(R.drawable.brand_tianmao);
                break;
            case "tiantian":
                iv_brand.setImageResource(R.drawable.brand_tiantian);
                break;
            case "weipinhui":
                iv_brand.setImageResource(R.drawable.brand_weipinhui);
                break;
            case "yousu":
                iv_brand.setImageResource(R.drawable.brand_yousu);
                break;
            case "youzheng":
                iv_brand.setImageResource(R.drawable.brand_youzheng);
                break;
            case "yuantong":
                iv_brand.setImageResource(R.drawable.brand_yuantong);
                break;
            case "yunda":
                iv_brand.setImageResource(R.drawable.brand_yunda);
                break;
            case "zhaijisong":
                iv_brand.setImageResource(R.drawable.brand_zhaijisong);
                break;
            case "zhongtong":
                iv_brand.setImageResource(R.drawable.brand_zhongtong);
                break;
            default:
                break;
        }
    }

    private void setTextViewBrand(String brandName, TextView tv_brand) {
        switch(brandName){
            case "anneng":
                tv_brand.setText("安能");
                break;
            case "baishi":
                tv_brand.setText("百世");
                break;
            case "debang":
                tv_brand.setText("德邦");
                break;
            case "ems":
                tv_brand.setText("EMS");
                break;
            case "guotong":
                tv_brand.setText("国通");
                break;
            case "jingdong":
                tv_brand.setText("京东");
                break;
            case "kuaijie":
                tv_brand.setText("快捷");
                break;
            case "quanfeng":
                tv_brand.setText("全峰");
                break;
            case "shentong":
                tv_brand.setText("申通");
                break;
            case "shunfeng":
                tv_brand.setText("顺丰");
                break;
            case "suning":
                tv_brand.setText("苏宁");
                break;
            case "tianmao":
                tv_brand.setText("天猫");
                break;
            case "tiantian":
                tv_brand.setText("天天");
                break;
            case "weipinhui":
                tv_brand.setText("唯品会");
                break;
            case "yousu":
                tv_brand.setText("优速");
                break;
            case "youzheng":
                tv_brand.setText("邮政");
                break;
            case "yuantong":
                tv_brand.setText("圆通");
                break;
            case "yunda":
                tv_brand.setText("韵达");
                break;
            case "zhaijisong":
                tv_brand.setText("宅急送");
                break;
            case "zhongtong":
                tv_brand.setText("中通");
                break;
            default:
                break;
        }
    }

}
